package cn.bdqn.layui.service.order;

import cn.bdqn.layui.entity.Order;
import cn.bdqn.layui.entity.User;
import cn.bdqn.layui.entity.vo.UserVO;
import cn.bdqn.layui.mapper.OrderMapper;
import cn.bdqn.layui.mapper.UserMapper;
import cn.bdqn.layui.service.product.ProductService;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.apache.ibatis.annotations.Param;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Service
@Component
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderMapper orderMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    ProductService productService;

    @Override
    public int addOrder(Order order) {
        return orderMapper.addOrder(order);
    }

    /**
     * 创建订单：把商品信息以及库存数量存入redis list set
     * 下单后查询redis该商品的库存，并判断库存后再执行下单业务逻辑
     * 下单成功后，将下单信息发送到rabbitmq消息队列，
     * 通过websockt通知空闲的帮客处理订单，把订单信息的通知
     * 通过短信的方式发送给帮客。帮客接单确认成功后再更新订单表里面的bangkeId字段
     * 订单处理完毕之后，给出相应评价，评分，以及佣金结算。
     * 后续继续优化商品秒杀功能
     */
    @Override

    @RabbitListener(queues = "order")
    public void addOrderSave(Order order) {
        // 通过websockt通知空闲的帮客处理订单，把订单信息的通知
        //查询空闲的帮客
        List<UserVO> byBangKe = userMapper.findByBangKe(0);
        //查询用户信息
        User user = userMapper.findByUserName("", order.getUserId());
        //随机数
        Random random = new Random();
        int nextInt = random.nextInt(byBangKe.size());

        //初始化sdk 发送短信通知用户
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        //初始化服务器和端口
        sdk.init("app.cloopen.com", "8883");
        sdk.setAccount("8a216da873369abc017345af059e0379", "6099e32376d54917bf289f0b6221704b");
        //初始化应用id
        sdk.setAppId("8a216da873369abc017345af06aa0380");
        HashMap<String, Object> stringObjectHashMap = sdk.sendTemplateSMS(user.getPhone(), "1", new String[]{"帮客" + byBangKe.get(nextInt).getUsername() + "收到您的订单，正在派送"});
        if ("000000".equals(stringObjectHashMap.get("statusCode"))) {
            UserVO userVO = new UserVO();
            //获取id
            userVO.setId(byBangKe.get(nextInt).getId());
            userVO.setBangkeStatic(1);
            if (userMapper.updateUser(userVO) > 0) {
                order.setBangkeid(userVO.getId());
                //修改订单帮客
                orderMapper.updateOrderByBangkeId(order.getBangkeid(), order.getId());
              /*  //修改库存
                productService.updateStack(order.getProductNum(), order.getProductId());*/
                //用户金额修改
                User byUserName = userMapper.findByUserName(null, order.getUserId());
                userVO.setMoney(byUserName.getMoney().subtract(order.getProductPrice()));
                userVO.setId(byUserName.getId());
                int i = userMapper.updateUser(userVO);
                if (i > 0) {
                    userVO.setBangkeStatic(0);
                    userMapper.updateUser(userVO);
                }
            }
        }

    }

    @Override
    public List<Order> findOrderAll(@Param("id") Integer id, @Param("userId") Integer userId, @Param("productName") String productName, @Param("page") Integer page, @Param("pageSize") Integer pageSize) {
        return orderMapper.findOrderAll(0, userId, productName, page, pageSize);
    }

    @Override
    public int findOrderCount(Integer id, Integer userId, String productName) {
        return orderMapper.findOrderCount(id, userId, productName);
    }

    @Override
    public Order findOrderById(Integer id) {
        return orderMapper.findOrderById(id);
    }
}
